package psn.hui.stat.utils;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;

import psn.hui.bus.domain.Customer;

public class ExportCustomerUtils {
	
	private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	
	/**
	 * 	导出客户数据
	 * @param customers
	 * @param fileName
	 * @param sheetName
	 * @param response
	 */
	public static void exportCustomer(List<Customer> customers, String fileName, String sheetName,
			HttpServletResponse response) {
		//第一步.组装excel文档
		//1.创建工作薄
		HSSFWorkbook workbook = new HSSFWorkbook();
		//2.获取样式
		HSSFCellStyle titleCellStyle = ExportHSSFCellStyle.createTitleCellStyle(workbook);
		HSSFCellStyle subTitleCellStyle = ExportHSSFCellStyle.createSubTitleCellStyle(workbook);
		HSSFCellStyle tableHeadCellStyle = ExportHSSFCellStyle.createTableHeadCellStyle(workbook);
		HSSFCellStyle tableRowCellStyle = ExportHSSFCellStyle.createTableRowCellStyle(workbook);
		//3.创建sheet
		HSSFSheet sheet = workbook.createSheet(sheetName);
		//4.设置sheet默认参数
		sheet.setDefaultColumnWidth(25);
		//5.合并标题行和副标题
		CellRangeAddress region_title = new CellRangeAddress(0, 0, 0, 6);
		sheet.addMergedRegion(region_title);
		CellRangeAddress region_subtitle = new CellRangeAddress(1, 1, 0, 6);
		sheet.addMergedRegion(region_subtitle );
		//6.创建标题行,设置样式，填入内容
		int row_index = 0;
		HSSFCell title_cell = sheet.createRow(row_index++).createCell(0);
		title_cell.setCellStyle(titleCellStyle);
		title_cell.setCellValue("汽车出租系统-客户数据");
		//7.创建副标题行,设置样式，填入内容
		HSSFCell subtitle_cell = sheet.createRow(row_index++).createCell(0);
		subtitle_cell.setCellStyle(subTitleCellStyle);
		subtitle_cell.setCellValue("总条数:"+customers.size()+" 导出时间:"+dateFormat.format(new Date()));
		//8.创建表头行，设置样式，填入内容
		String[] titles = {"身份证号","客户姓名","性别","客户电话","客户地址","客户职位","录入时间"};
		HSSFRow thead_row = sheet.createRow(row_index++);
		for (int i = 0; i < titles.length; i++) {
			HSSFCell thead_cell = thead_row.createCell(i);
			thead_cell.setCellStyle(tableHeadCellStyle);
			thead_cell.setCellValue(titles[i]);
		}
		//9.填充表格数据
		for (Customer customer : customers) {
			HSSFRow tbody_row = sheet.createRow(row_index++);
			HSSFCell tbody_identity = tbody_row.createCell(0);
			tbody_identity.setCellStyle(tableRowCellStyle);
			tbody_identity.setCellValue(customer.getIdentity());
			
			HSSFCell tbody_custname = tbody_row.createCell(1);
			tbody_custname.setCellStyle(tableRowCellStyle);
			tbody_custname.setCellValue(customer.getCustname());
			
			HSSFCell tbody_sex = tbody_row.createCell(2);
			tbody_sex.setCellStyle(tableRowCellStyle);
			tbody_sex.setCellValue(customer.getSex()==1?"男":"女");
			
			HSSFCell tbody_phone = tbody_row.createCell(3);
			tbody_phone.setCellStyle(tableRowCellStyle);
			tbody_phone.setCellValue(customer.getPhone());
			
			HSSFCell tbody_address = tbody_row.createCell(4);
			tbody_address.setCellStyle(tableRowCellStyle);
			tbody_address.setCellValue(customer.getAddress());
			
			HSSFCell tbody_career = tbody_row.createCell(5);
			tbody_career.setCellStyle(tableRowCellStyle);
			tbody_career.setCellValue(customer.getCareer());
			
			HSSFCell tbody_createtime = tbody_row.createCell(6);
			tbody_createtime.setCellStyle(tableRowCellStyle);
			tbody_createtime.setCellValue(dateFormat.format(customer.getCreatetime()));
		}
		//第二步.导出下载
		try {
			//处理中文文件名编码
			fileName = URLEncoder.encode(fileName, "UTF-8");
			//设置下载的文件的名称
			response.setContentType("application/vnd.ms-excel;charset=utf-8");
			response.setCharacterEncoding("UTF-8");
			response.setHeader("Content-Disposition", "attachment;filename="+fileName);
			workbook.write(response.getOutputStream());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
